package hello;

import java.util.concurrent.CountDownLatch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

public class Receiver  {
    private static final Logger LOGGER = LoggerFactory.getLogger(Receiver.class);

    private CountDownLatch latch;

    @Autowired
    public Receiver(CountDownLatch latch) {
        this.latch = latch;
    }

//    @Override
//    public void onMessage(Message message, byte[] bytes) {
//        RedisSerializer serializer = new JdkSerializationRedisSerializer();
//        LOGGER.info("Received <" + serializer.deserialize(message.getBody()) + ">");
//        latch.countDown();
//    }


    public void receiveMessage(String o) {
        RedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);

        System.out.println(o);
        hello.Message message = (hello.Message) serializer.deserialize(o.getBytes());
        LOGGER.info("Received <" + message + ">");
        latch.countDown();
    }
}
